//-----------------------------------------------------------------------
// <copyright file="TextMessage.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.Windows.Browser;

    /// <summary>
    /// Represents a text message.
    /// </summary>
    public sealed class TextMessage : Message
    {
        private readonly MessengerEntity entity;

        private readonly TextMessageFormat format;

        /// <summary>
        /// Initializes a new instance of the <see cref="TextMessage"/> class.
        /// </summary>
        /// <param name="text">The text message content.</param>
        public TextMessage(
            string text)
            : this(text, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TextMessage"/> class.
        /// </summary>
        /// <param name="text">The text message content.</param>
        /// <param name="format">The text message format.</param>
        public TextMessage(
            string text,
            TextMessageFormat format)
        {
            if (String.IsNullOrEmpty(text))
            {
                throw ExceptionHelper.CreateArgumentNullException("text");
            }

            if (format == null)
            {
                format = TextMessageFormat.DefaultFormat;
            }

            this.entity = MessengerEntity.CreateInstance(typeof(TextMessage), text, format.Entity);
            this.format = format;
        }

        internal TextMessage(MessengerEntity entity)
        {
            this.entity = entity;
            this.format = new TextMessageFormat(this.entity.GetProperty("Format"));
        }

        internal override MessengerEntity Entity
        {
            get { return this.entity; }
        }

        /// <summary>
        /// Gets the text message format.
        /// </summary>
        public TextMessageFormat Format
        {
            get { return this.format; }
        }

        /// <summary>
        /// Gets the text message content.
        /// </summary>
        public string Text
        {
            get { return this.entity.GetProperty<string>("Text"); }
        }

        /// <summary>
        /// Creates a DOM element containing the contents of the text message.
        /// </summary>
        /// <remarks>
        /// The generated DOM element will be a SPAN.
        /// </remarks>
        public HtmlElement CreateTextElement()
        {
            return (HtmlElement)this.entity.Invoke("CreateTextElement");
        }
    }
}